package cn.topcodes.tcsf.app.web.controller.api;

import cn.topcode.unicorn.utils.ValidationUtil;
import cn.topcodes.tcsf.admin.commons.api.domain.ApiResponseEntity;
import cn.topcodes.tcsf.admin.commons.domain.Page;
import cn.topcodes.tcsf.app.domain.entity.Advice;
import cn.topcodes.tcsf.app.domain.entity.AppUser;
import cn.topcodes.tcsf.app.service.AdviceService;
import cn.topcodes.tcsf.app.service.AppUserService;
import cn.topcodes.tcsf.app.web.controller.api.protocol.SubmitAdvice;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

/**
 * 反馈建议
 * @author Unicorn
 * @date 2017/5/5
 */
@Api(tags = {"APP通用接口(建议反馈)"})
@RestController
@RequestMapping(value = "/api/app/advice", produces = "application/json")
public class AdviceApi {

    @Autowired
    private AppUserService appUserService;

    @Autowired
    private AdviceService adviceService;

    @ApiOperation("获取个人反馈建议历史列表")
    @RequestMapping(value = "page-list",method = RequestMethod.GET)
    public ApiResponseEntity pageList(
            @RequestHeader(required = true) String token,
            @RequestParam(required = false, name = "page", defaultValue = "1") Integer pageNumber,
            @RequestParam(required = false, name = "size", defaultValue = "20") Integer pageSize
    ) {
        AppUser appUser = this.appUserService.findByToken(token);
        Page<Advice> advicePage = this.adviceService.findPageBy(appUser.getId(), pageNumber, pageSize);
        return ApiResponseEntity.buildOK(advicePage);
    }

    @ApiOperation("个人反馈建议详情")
    @RequestMapping(value = "detail",method = RequestMethod.GET)
    public ApiResponseEntity detail(
            @RequestHeader String token,
            @RequestParam Long id
    ) {
        AppUser appUser = this.appUserService.findByToken(token);
        Advice advice = this.adviceService.findOne(id);
        if(!appUser.getId().equals(advice.getAppUserId())) {
            return ApiResponseEntity.buildPermissionDenied("您没有权限查看");
        }
        return ApiResponseEntity.buildOK(advice);
    }

    @ApiOperation("提交反馈建议")
    @RequestMapping(value = "submit",method = RequestMethod.POST)
    public ApiResponseEntity submit(
            @RequestHeader String token,
            @Valid @RequestBody SubmitAdvice req,
            BindingResult result
    ) {
        ValidationUtil.validate(result);
        AppUser appUser = this.appUserService.findByToken(token);
        Advice advice = new Advice();
        advice.setAppUserId(appUser.getId());
        advice.setUsername(appUser.getUsername());
        advice.setRealName(appUser.getRealName());
        advice.setEmail(appUser.getEmail());
        advice.setTel(appUser.getTel());
        advice.setContent(req.getContent());
        this.adviceService.create(advice);
        return ApiResponseEntity.buildOK();
    }
}
